rm(list=ls())
library(foreign)
library(ggplot2) 
library(gridExtra)
library(knitr)
library(grid)
library(dplyr)
library(forcats)
#install.packages("extrafont")
library(extrafont)
#install.packages("performance")
library("performance")
#install.packages("see")
library("see")
library("haven")

######### Binary Treatment -------
setwd("~/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Estimates")
setwd("/Users/sergimartinez/Dropbox/EUI/BET HOUSES PROJECT/data/estimates_050121/")

####### Setting the theme:

theme_results <- theme_bw(base_size = 15) +
  theme(text=element_text(family="Times",
                          size = 15),
        axis.text.x = element_text(size = 15, color = "black"),
        axis.text.y = element_text(size = 15, color = "black",angle=0),
        axis.title.y = element_text(size = 15, color = "black",angle=0,vjust=1),
        axis.title.x = element_text(size = 15, color = "black"),
        legend.position = "top",
        legend.key.size = unit(1.2, "cm"),
        legend.text = element_text(size = 15),
        legend.title = element_text(size = 15),
        panel.grid.major.y = element_blank(),
        panel.grid.minor.y = element_blank(),
        strip.text.y.right = element_text(angle = 0))


all <- read_dta("Price2.dta")

all$id2 <- factor(all$inc,
                  levels = c(0,1,2),
                  labels = c("Below average","All", "Above average"))


all$tipo <- factor(all$out,
                   levels=0,
                   labels="Rent Prices in euros/m2")

all$y2 <- factor(all$charter,
                 levels = c(0,1,2),
                 labels = c("Only charter \nhigh-schools",
                            "Only public \nhigh-schools",
                            "Overall average"))

library(dplyr)

all$charter2 <- all$charter + 1
all$charter2[all$charter2>2] <- 0

all$y2 <- factor(all$charter2,
                 levels = c(0,1,2),
                 labels = c("Overall average",
                            "Only charter \nhigh-schools",
                            "Only public \nhigh-schools"))


all$year <- factor(all$Year,
                   levels = c(0,1,2,3),
                   labels = c("Placebo t-2", "Placebo, t-1", "Effect for the\n Opening Year", "Effect on the First\nFull-Open Course"))


palette2 = c("black", "blue", "red")

figureS10 <- all[all$out==0 & all$Year>0,] %>%
  ggplot(aes(x = y2, y = coef, colour = id2)) +   
  geom_point(size = 2 ,position = position_dodge(1)) +
  geom_errorbar(aes(ymax = ci_upper95, ymin = ci_lower95), width = 0,  size = .7, position = position_dodge(width = 1)) +
  geom_errorbar(aes(ymax = ci_upper90, ymin = ci_lower90), width = 0, size = 1.6, position = position_dodge(width = 1)) +
  scale_color_manual(name = "Neighborhood\nby income levels:",values = palette2)+
  geom_hline(yintercept = 0, 
             linetype = 2, color = "black") + 
  xlab("") +
  ylab("Effect of new betting houses opening on rent prices") +
  theme_results + 
  coord_flip() +
  facet_grid(year~., scales="free_x")+
  theme(strip.background =element_rect(fill="white"))
figureS10

#ggsave("130121_results_all_prices.png", plot = plotall, width = 40, height = 25, units = "cm")
